doexec()
{
    tag=$1
    cp ../extractHist$tag .

    cat > subdo$tag.sh <<EOF
uname -a

source /hera/alice/xlu/alice/script/hera-evn.sh -n
cd $dd

echo ldd
ldd extractHist$tag
echo

echo ls
ls
echo

echo pwd
pwd
echo

echo go
./extractHist$tag $dir0/$ii > see$tag.log

echo done
EOF
    qsub -cwd -l h_rt=10:0:0,h_rss=2G -P alice ./subdo$tag.sh
}

#########################################

#######################################
####### Modified compared to r 1727 !!! should check for official analysis
#######################################

ktest=
#"test-"
kgen=0
#1

#only used for efficiency -> PRIM
#otherwise - SECO, namely after the general track cuts

dir0=$(pwd)

for ii in $(find list.* -type l | grep -v 10e | grep -v 10-d-e)
#for ii in $(find list.*10dMC* -type l)

#for ii in $(find list.LHC10-d-e* -type l)
#for ii in $(find list.LHC11a_without_SDD.pass4*)
#for ii in $(find list.LHC11aMC.txt -type l)

do
    jj=$(echo $ii | awk -F\. '{print $2}')

    if [ ${jj:0:6} == LHC10h ]
    then
        dirs=$(echo $(pwd)/$ktest$jj\CEN{0,1,2,3,4,5}_RAA_ETA{0,1,2,3}{"","_POS","_NEG"})
    else
        dirs=$(echo $(pwd)/$ktest$jj\_RAA_ETA{0,1,2,3}{"","_POS","_NEG"})
    fi

    for dd in $dirs
    do 
        if [ ${jj:6:2}k == MCk -a $kgen == 1 ]
        then
            #efficiency need prim cut
            #dd=$dd\_PRIM

            #general
            dd=$dd\_SECO
        fi

        echo $dd
        mkdir $dd
        cd $dd
        cp ~/svn.log .

        doexec Rec

        if [ ${jj:6:2}k == MCk -a $kgen == 1 ]
        then
            doexec Gen
        fi

        cd $dir0
    done
done

exit

###############################################

ana()
{
    mkdir -p  $dirs
    
    for ii in $dirs
    do 
        echo $ii
        cd $ii
        cp ~/svn.log .; 
#Rec is enough for secondary study for DATA
        $(pwd)/../extractHistRec ../$file > seeRec.log 
        #nohup $(pwd)/../extractHistRec ../$file > seeRec.log &
        #nohup $(pwd)/../extractHistGen ../$file > seeGen.log &
        cd -
    done
}


kETA=$1

charge=$2
#_POS
#_NEG
#
#


file=list.LHC10e.pass2
dirs=$(echo $ktest\10eDATA_RAA_ETA$kETA$charge )
#dirs=$(echo $ktest\10eDATA_RAA_ETA3$charge )
ana

file=list.LHC10d.pass2
dirs=$(echo $ktest\10dDATA_RAA_ETA$kETA$charge )
ana

file=list.LHC10-d-e
dirs=$(echo $ktest\10-d-e-DATA_RAA_ETA$kETA$charge )
ana

file=list.LHC10h.pass2
dirs=$(echo $ktest\10hCEN{0,1,2,3,4,5}DATA_RAA_ETA$kETA$charge )
ana

file=list.LHC11a_without_SDD.pass4
dirs=$(echo $ktest\11aDATA_RAA_ETA$kETA$charge )
ana

exit

file=anchorLHC11a_MC_LHC11b10a.list
#dirs=$(echo $ktest\11aMC_RAA_ETA{0,1,2,3}_PRIM$charge )
dirs=$(echo $ktest\11aMC_RAA_ETA3_SECO)
ana

file=anchorLHC10h_MC_LHC11a10a_bis.list
dirs=$(echo $ktest\10hCEN{0,1,2,3,4,5,6}MCmb_RAA_ETA{0,1,2,3}_PRIM$charge )
#dirs=$(echo $ktest\10hCEN0MCmb_RAA_ETA3_SECO)
ana


file=anchorLHC10e_MC_LHC10e21_LHC10e20.list
dirs=$(echo $ktest\10eMC_RAA_ETA{0,1,2,3}_PRIM$charge )
#dirs=$(echo $ktest\10eMC_RAA_ETA3_SECO)
ana

file=anchorLHC10d_MC_LHC10f6a_LHC10f6.list
dirs=$(echo $ktest\10dMC_RAA_ETA{0,1,2,3}_PRIM$charge )
#dirs=$(echo $ktest\10dMC_RAA_ETA3_SECO)
ana

exit

file=anchor10h_MC_PbPbLHC11a10b_bis.list
dirs=$(echo $ktest\10hMChighpt_RAA_{ETA0,ETA3}_PRIM)
ana

############################################################################
###########################################################################

#generate fraction from PT
for ii in $(find 1*MC* -type d | grep -v "/"); do echo $ii; cd $ii; /u/xlu/.task/2dbetabloch/getMCtrueFromPT histRec*.root 20 > seegetMCtrue20.log ;cd -; done

#generate Efficiency
for ii in $(find 1*MC*PRIM -type d | grep -v "/")
do
    echo $ii
    cd $ii
    ~/.task/2dbetabloch/calcEffMC histGen* histRec* mcpdgVsPt > seeEffPt.log
    ~/.task/2dbetabloch/calcEffMC histGen* histRec* mcpdgVsEta > seeEffEta.log
    cd -
done

#coherent fit; it takes time
method=saturation
#method=lund
for ii in $(find 1*MC* -type d | grep -v "/")
do 
    for jj in $(echo anchor3)
    do
        dir=$ii/coherent_$method\_$jj\_HASENERGYLOSSCORRECTION
        echo $dir

        mkdir $dir
        cd $dir    
        
        cp ~/svn.log .; cp /u/xlu/.task/2dbetabloch/{fith,mn2d} .
        /u/xlu/.task/2dbetabloch/dofith.sh ../histRec*.root
        /u/xlu/.task/2dbetabloch/domn2d.sh 3000 2
        cd -
    done
done

#drawpar on coherent fit output
for ii in $(find 1*MC*/coherent_$method* -type d | grep "/ID" | grep -v save)
do 
    echo $ii
    cd $ii
    ~/.task/2dbetabloch/dodrawpar.sh
    cd -
done

#getGrSys on drawpar output
for ii in $(find 1*MC*/coherent_$method* -type d | grep "/ID" -v | grep -v save | grep -v Sys)
do 
    dir=$ii/Sys
    echo $dir
    mkdir $dir
    cd $dir
    find ../ID*/parm*iBeta100*.root > in.log
    ~/.task/2dbetabloch/getGrSys in.log 20 > see20.log
    cd -
done

###########################################################################
###########################################################################


#draw secondary fraction vs. pT
for ii in  $(find 1*SECO* -type d | grep -v "/"); 
do 
    jj=$(echo $ii| awk -F\_ '{$4="";print}'|tr -s ' ' '_'); 
    dir=checkSecoFrac_$jj

    echo $dir
    mkdir $dir; 
    cd $dir

file=$(find ../$jj\SECO/histRec*.root | grep -v coherent)
#root -b -l -q '~/.task/2dbetabloch/plotcode/drawSecoFrac.C("'$file'")'
echo $file
cd -
done

#compare fraction between PRIM and SECO, to see whether the primary cut is working
for ii in  $(find 1*PRIM* -type d | grep -v "/"); 
do 
    jj=$(echo $ii| awk -F\_ '{$4="";print}'|tr -s ' ' '_'); 

    dir=checkPrimaryCut_$jj

    echo $dir
    mkdir $dir; 

    cd $dir;
file1=$(find ../$jj\SECO/from*.root)
file2=$(find ../$jj\PRIM/from*.root)
echo $file1
echo $file2
echo

for kk in $(seq 0 3)
do 
    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("'$kk'","'$file1'","'$file2'", 0, '$kk')'
done

    cd -;

done

#compare efficiency between MIDETA and LARGE, just to see whether consistent
for ii in  $(find 1*LARGE_PRIM* -type d | grep -v check | grep -v "/"); 
do 
    jj=$(echo $ii| awk -F\_ '{$3="";$4="";print}'| tr -s '_' '_' | tr -s ' ' '_'); 
    echo $jj

    dir=checkEtaEfficiency_$jj

    echo $dir
    mkdir $dir; 

    cd $dir;
file1=$(find ../$jj\LARGE_PRIM/OutEff-mcpdgVsPt.root)
file2=$(find ../$jj\MIDETA_PRIM/OutEff-mcpdgVsPt.root)
echo $file1
echo $file2
echo

for kk in $(seq 0 3)
do 
    root -b -l -q '~/.task/2dbetabloch/plotcode/drawEff.C("'$file1'","'$file2'", '$kk')'
done

cd -;
done


#p2pT

dir=checkp2pT_10hMChighpt_RAA_LARGE_SECO
hist=$(echo ../10hMChighpt_RAA_LARGE_SECO/histRec*.root)
echo $hist

mkdir $dir
cd $dir

ln -s $hist hist.root
 ~/.task/2dbetabloch/getMCtrueFromMOM hist.root  0 1 > seegetfromMOM.log
~/.task/2dbetabloch/drawpar MCtrue_save*.root > seedrawpar.log
find parMCtrue*.root > in.log
~/.task/2dbetabloch/getGrSys in.log 20 > seegetMOM20.log
ln -s outgrsys_10hMC_id20.root fromMOMoutgrsys_10hMC_id20.root
ln -s ../10hMChighpt_RAA_LARGE_SECO/from*.root

file1=$(find fromMOM*.root)
file2=$(find fromPT*.root)
echo $file1
echo $file2
echo

for kk in $(seq 0 3)
do 
    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("p2pT","'$file1'","'$file2'", 0, '$kk')'
done

cd -


#compare fromPT and coherent fit result
for ii in $(find 1*MC*/coherent_$method*/Sys -type d )
do 
    fitfile=$(echo $(pwd)/$ii/*.root)
    if [ ! -e $fitfile ] 
    then
        continue
    fi

    #echo $fitfile

    truefile=$(echo $(pwd)/$ii/../../fromPT*.root)
    #echo $truefile
    
    hh=$(echo $ii | awk -F\/ '{print $1"-"$2}')
    dir=checkFit/$hh
    echo $dir

    mkdir $dir
    cd $dir
    ln -s $fitfile $hh$(echo $fitfile |  awk -F\/ '{print $NF}')
    ln -s $truefile $hh$(echo $truefile |  awk -F\/ '{print $NF}')

file1=$(find *out*.root)
file2=$(find *from*.root)
echo $file1
echo $file2
echo

for kk in $(seq 0 3)
do 
    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("FIT","'$file1'","'$file2'", 0, '$kk')'
done

    cd -
done > seeCheckFit

cd checkFit
mkdir good bad
mv $(grep ierr ../1*MC*/coherent_$method*anchor3*/ID3000_2/see.log | grep "nbeta 100" | grep "ierr 4" | awk -F\/ '{print $2"-"$3}') bad/
mv $(grep ierr ../1*MC*/coherent_$method*anchor3*/ID3000_2/see.log | grep "nbeta 100" | grep "ierr 0" | awk -F\/ '{print $2"-"$3}') good/

cd -

exit

###################################################################################
####### fit DATA!!
method=saturation
#method=lund
for ii in $(find 1*DATA* -type d | grep -v "/")
do 
    for jj in $(echo anchor3)
    do
        dir=$ii/coherent_$method\_$jj\_HASENERGYLOSSCORRECTION
        echo $dir

        mkdir $dir
        cd $dir    
        
        cp ~/svn.log .; cp /u/xlu/.task/2dbetabloch/{fith,mn2d} .
        /u/xlu/.task/2dbetabloch/dofith.sh ../histRec*.root
        /u/xlu/.task/2dbetabloch/domn2d.sh 3000 2
        cd -
    done
done

#drawpar on coherent fit output
for ii in $(find 1*DATA*/coherent_$method* -type d | grep "/ID" | grep -v save)
do 
    echo $ii
    cd $ii
    ~/.task/2dbetabloch/dodrawpar.sh
    cd -
done

#getGrSys on drawpar output
for ii in $(find 1*DATA*/coherent_$method* -type d | grep "/ID" -v | grep -v save | grep -v Sys)
do 
    dir=$ii/Sys
    echo $dir
    mkdir $dir
    cd $dir
    find ../ID*/parm*iBeta100*.root > in.log
    ~/.task/2dbetabloch/getGrSys in.log 20 > see20.log
    cd -
done

#compare LUND and coherent fit result
for ii in $(find 1*DATA*/coherent_$method*/Sys -type d )
do 
    fitfile=$(echo $(pwd)/$ii/*.root)
    if [ ! -e $fitfile ] 
    then
        continue
    fi

    jj=$(echo $ii|tr -s '/' '_');
    dir=checkDataFit/$jj
    mkdir $dir
    cd $dir;

    echo $fitfile

    file1=$fitfile

    root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("data","'$file1'","", 1)'

    #no electron
    for kk in $(seq 0 2)
    do 
        root -b -l -q '~/.task/2dbetabloch/plotcode/drawSys.C("dataipar","'$file1'","", 1, '$kk')'
    done

    ls;
    cd -;
done > seeCheckDataFit
